<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------

namespace app\adminapi\lists\read_uv;


use app\adminapi\lists\BaseAdminDataLists;
use app\adminapi\logic\auth\AdminLogic;
use app\common\model\promotion\PromotionLink;
use app\common\model\read_uv\ReadUv;
use app\common\lists\ListsSearchInterface;


/**
 * ReadUv列表
 * Class ReadUvLists
 * @package app\adminapi\lists
 */
class ReadUvLists extends BaseAdminDataLists implements ListsSearchInterface
{


    /**
     * @notes 设置搜索条件
     * @return \string[][]
     * @author likeadmin
     * @date 2024/04/22 15:52
     */
    public function setSearch(): array
    {
        return [
            '%like%' => ['book_name'],
            '=' => ['platform', 'book_id'],
        ];
    }


    /**
     * @notes 获取列表
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author likeadmin
     * @date 2024/04/22 15:52
     */
    public function lists(): array
    {
        $sort = $this->params['sort'] ?? 0;
        switch ($sort){
            case 0:
                $sortArr['update_time'] = 'desc';
                break;
            case 1:
                $sortArr['date'] = 'desc';
                $sortArr['chapter_order'] = 'asc';
                break;
            case 2:
                $sortArr['date'] = 'desc';
                $sortArr['chapter_order'] = 'desc';
                break;
            default:
                $sortArr['update_time'] = 'desc';
                break;
        }
        return ReadUv::where($this->searchWhere)->where($this->queryWhere())
            ->field(['id', 'promotion_link_id','date', 'platform', 'book_id', 'book_name', 'chapter_id', 'chapter_name', 'chapter_order', 'sum(read_uv) as read_uv', 'sum(new_user_read_uv) as new_user_read_uv'])
            ->limit($this->limitOffset, $this->limitLength)
            ->order($sortArr)
            ->group('chapter_id')
            ->select()
            ->toArray();
    }

    /**
     * @notes 搜索条件
     * @author 段誉
     * @date 2023/2/24 16:08
     */
    public function queryWhere()
    {
        $where = [];
        // 下单时间
        if (!empty($this->params['start_time']) && !empty($this->params['end_time'])) {
            $time    = [$this->params['start_time'], $this->params['end_time']];
            $where[] = ['date', 'between', $time];
        }

        if ($this->adminInfo['root'] != 1) {
            $pAdminIds = AdminLogic::getPermissionAdminIds($this->adminInfo);
            if ($pAdminIds) {
                $promotion_link_id_arr = PromotionLink::where('admin_id','in',$pAdminIds)->column('id');
                if ($promotion_link_id_arr){
                    $where[] = ['promotion_link_id', 'in', $promotion_link_id_arr];
                }
            }
        }

        return $where;
    }


    /**
     * @notes 获取数量
     * @return int
     * @author likeadmin
     * @date 2024/04/22 15:52
     */
    public function count(): int
    {
//        return ReadUv::where($this->searchWhere)->where($this->queryWhere())->count();
        return ReadUv::where($this->searchWhere)->where($this->queryWhere())
            ->field(['id', 'promotion_link_id','date', 'platform', 'book_id', 'book_name', 'chapter_id', 'chapter_name', 'chapter_order', 'sum(read_uv) as read_uv', 'sum(new_user_read_uv) as new_user_read_uv'])
            ->group('chapter_id')
            ->select()->count();
    }

}